﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.Files.Authorization;
using LeadThing.AbpZeroTemplate.Files.Dtos;
using LeadThing.AbpZeroTemplate.Dto;


namespace LeadThing.AbpZeroTemplate.Files
{
    /// <summary>
    /// 文件服务实现
    /// </summary>



    public class FileAppService : AbpZeroTemplateAppServiceBase, IFileAppService
    {
        private readonly IRepository<File, long> _fileRepository;
        private readonly IFileListExcelExporter _fileListExcelExporter;


        private readonly FileManage _fileManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public FileAppService(IRepository<File, long> fileRepository,
FileManage fileManage
      , IFileListExcelExporter fileListExcelExporter
  )
        {
            _fileRepository = fileRepository;
            _fileManage = fileManage;
            _fileListExcelExporter = fileListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<File> _fileRepositoryAsNoTrack => _fileRepository.GetAll().AsNoTracking();


        /// <summary>
        /// 新增文件并返回新增ID
        /// </summary>
        public virtual int CreateFileAsyncReturnID(FileEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<File>();

            entity.Id = _fileRepository.InsertOrUpdateAndGetId(entity);
            return Convert.ToInt32(entity.Id);
        }

        /// <summary>
        /// 根据条件获取所有文件数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ListResultDto<FileListDto>> GetAllFilesAsync(GetFileInput input)
        {
            var query = _fileRepositoryAsNoTrack;

            //TODO:根据传入的参数添加过滤条件
            #region

            if (input.DisID != 0)
            {
                query = query.Where(p => p.DisID == input.DisID);
            }

            #endregion

            var files = await query
            .OrderByDescending(p => p.Id)
            .ToListAsync();

            return new ListResultDto<FileListDto>(
                files.Select(item =>
                {
                    var dto = item.MapTo<FileListDto>();
                    return dto;
                }).ToList());
        }

        #endregion


        #region 文件管理

        /// <summary>
        /// 根据查询条件获取文件分页列表
        /// </summary>
        public async Task<PagedResultDto<FileListDto>> GetPagedFilesAsync(GetFileInput input)
        {

            var query = _fileRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var fileCount = await query.CountAsync();

            var files = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var fileListDtos = files.MapTo<List<FileListDto>>();
            return new PagedResultDto<FileListDto>(
            fileCount,
            fileListDtos
            );
        }

        /// <summary>
        /// 通过Id获取文件信息进行编辑或修改 
        /// </summary>
        public async Task<GetFileForEditOutput> GetFileForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetFileForEditOutput();

            FileEditDto fileEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _fileRepository.GetAsync(input.Id.Value);
                fileEditDto = entity.MapTo<FileEditDto>();
            }
            else
            {
                fileEditDto = new FileEditDto();
            }

            output.File = fileEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取文件ListDto信息
        /// </summary>
        public async Task<FileListDto> GetFileByIdAsync(EntityDto<long> input)
        {
            var entity = await _fileRepository.GetAsync(input.Id);

            return entity.MapTo<FileListDto>();
        }







        /// <summary>
        /// 新增或更改文件
        /// </summary>
        public async Task CreateOrUpdateFileAsync(CreateOrUpdateFileInput input)
        {
            if (input.FileEditDto.Id.HasValue)
            {
                await UpdateFileAsync(input.FileEditDto);
            }
            else
            {
                await CreateFileAsync(input.FileEditDto);
            }
        }

        /// <summary>
        /// 新增文件
        /// </summary>
        public virtual async Task<FileEditDto> CreateFileAsync(FileEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<File>();

            entity.Id = await _fileRepository.InsertOrUpdateAndGetIdAsync(entity);
            return entity.MapTo<FileEditDto>();
        }

        /// <summary>
        /// 编辑文件
        /// </summary>
        public virtual async Task UpdateFileAsync(FileEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _fileRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _fileRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        public async Task DeleteFileAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _fileRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除文件
        /// </summary>
        public async Task BatchDeleteFileAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _fileRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 文件的Excel导出功能

        public async Task<FileDto> GetFileToExcel()
        {
            var entities = await _fileRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<FileListDto>>();

            var fileDto = _fileListExcelExporter.ExportFileToFile(dtos);



            return fileDto;
        }

        #endregion

        #region APP接口

        /// <summary>
        /// 新增文件并返回新增ID
        /// </summary>
        public int CreateFileAsyncReturnIDAPP(FileEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<File>();

            entity.Id = _fileRepository.InsertOrUpdateAndGetId(entity);
            return Convert.ToInt32(entity.Id);
        }

        /// <summary>
        /// 通过Id获取文件信息进行编辑或修改 
        /// </summary>
        public async Task<FileEditDto> GetFileForEditAPPAsync(long Id)
        {
            FileEditDto fileEditDto;
            if (Id > 0)
            {
                var entity = await _fileRepository.GetAsync(Id);
                fileEditDto = entity.MapTo<FileEditDto>();
            }
            else
            {
                fileEditDto = new FileEditDto();
            }
            return fileEditDto;
        }

        #endregion

    }
}
